
#include "tp2.h"
#include <math.h>

#define MIN(x,y) ( x < y ? x : y )
#define MAX(x,y) ( x > y ? x : y )

#define P 2

void mblur_c    (
    unsigned char *src,
    unsigned char *dst,
    int cols,
    int filas,
    int src_row_size,
    int dst_row_size)
{
    unsigned char (*src_matrix)[src_row_size] = (unsigned char (*)[src_row_size]) src;
    unsigned char (*dst_matrix)[dst_row_size] = (unsigned char (*)[dst_row_size]) dst;

    for (int i = 0; i < 2; i++) {
	for (int j = 0; j < 4*cols; j++) {
		dst_matrix[i][j] = 0;
		dst_matrix[i][j+1] = 0;
		dst_matrix[i][j+2] = 0;

	}
    }
	
    for (int i = 2; i < filas - 2; i++) {
	for (int j = 0; j < 8; j++) {
		dst_matrix[i][j] = 0;
		dst_matrix[i][j+1] = 0;
		dst_matrix[i][j+2] = 0;		
	}

	for (int j = 8; j < 4*cols - 8; j++) {
		unsigned int filamenos2columnanemos2 = src_matrix[i-2][j-8];
		unsigned int filamenos1columnanemos1 = src_matrix[i-1][j-4];
		unsigned int fila0columna0 = src_matrix[i][j];
		unsigned int filamas1columna1 =  src_matrix[i+1][j+4];
		unsigned int filamas2columnamas2 = src_matrix[i+2][j+8];
		unsigned int computo = filamenos2columnanemos2 + filamenos1columnanemos1 + fila0columna0 + filamas1columna1 + filamas2columnamas2;
		float resultado = ((float) computo) / 5.0;
		if (resultado > 255) {
			resultado = 255;
		} 
		unsigned int res = (unsigned int) round(resultado);
		dst_matrix[i][j] = res;
	}

	for (int j = 4*cols - 8; j < 4*cols; j++) {
		dst_matrix[i][j] = 0;
		dst_matrix[i][j+1] = 0;
		dst_matrix[i][j+2] = 0;		
	}

    }

    for (int i = filas -2; i < filas; i++) {
	for (int j = 0; j < 4*cols; j++) {
		dst_matrix[i][j] = 0;
		dst_matrix[i][j+1] = 0;
		dst_matrix[i][j+2] = 0;

	}
    }
 
}


